/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lucava.boatskeeper.client.presenter.user;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasValue;
import net.customware.gwt.dispatch.client.DispatchAsync;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.Place;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import org.lucava.boatskeeper.client.PlaceMapper;
import org.lucava.boatskeeper.client.UserSession;
import org.lucava.boatskeeper.client.event.GotoEvent;
import org.lucava.boatskeeper.client.presenter.ViewPresenter;
import org.lucava.boatskeeper.shared.User;
import org.lucava.boatskeeper.shared.rpc.user.ConnectUser;
import org.lucava.boatskeeper.shared.rpc.user.ConnectUserResult;

/**
 *
 * @author glenn
 */
public class UserAuthPresenter extends ViewPresenter<UserAuthPresenter.Display> {

    public interface Display extends WidgetDisplay {

        HasClickHandlers getConnectButton();

        public HasValue<String> getLogin();

        public HasValue<String> getPassword();

        public void setMessage(String message);
    }
    public static Place PLACE;
    private User user;

    public UserAuthPresenter(DispatchAsync dispatcher, EventBus eventBus, Display display) {
        super(dispatcher, eventBus, display);
        bind();
    }

    @Override
    protected void onBind() {
        display.getConnectButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                doConnect();
            }
        });
    }

    protected void doConnect() {
        user = new User();
        user.setLogin(display.getLogin().getValue());
        user.setPassword(display.getPassword().getValue());

        getDispatcher().execute(new ConnectUser(user), new AsyncCallback<ConnectUserResult>() {
            @Override
            public void onSuccess(ConnectUserResult result) {

                if (result.getData() != null) {
                    UserSession.getInstance().setAuthenticated(true);
                    eventBus.fireEvent(new GotoEvent(PlaceMapper.HOME_PAGE));
                } else {
                    UserSession.getInstance().setAuthenticated(false);
                    display.setMessage("Login ou mot de passe incorrect !");
                    user = null;
                }
            }

            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error connecting user :" + caught.getLocalizedMessage());
            }
        });
    }

    @Override
    protected void onPlaceRequest(PlaceRequest request) {


        if (user != null) {
            eventBus.fireEvent(new GotoEvent(PlaceMapper.HOME_PAGE));
        } else {
            refreshDisplay();
        }
    }

    @Override
    public void refreshDisplay() {

        display.getLogin().setValue(null);
        display.getPassword().setValue(null);

    }

    @Override
    public Place getPlace() {
        return PLACE;
    }
}
